//
//  LoginViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "LoginViewController.h"
#import "MCMainTabBarController.h"
#import "RegisterViewController.h"


@interface LoginViewController ()

@property (nonatomic,strong)  UIButton * loginBtn;
@property (nonatomic,strong)  UITextField * passwordTextField;
@property (nonatomic,strong)  UITextField * phoneTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
}

#pragma mark=============setProperty=============
-(void)setProperty{
   
    self.view.backgroundColor=[UIColor whiteColor];
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;

    self.navigationController.navigationBarHidden=YES;
}

-(void)createUI{
    
    [self createBackBtn];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"用户登录";
    label.font = [UIFont systemFontOfSize:30];
    label.textColor = RGB(30, 63, 89);
    [self.view addSubview:label];
    label.textAlignment = NSTextAlignmentLeft;
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(80);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.width.equalTo(@(200));
        make.height.equalTo(@(40));
    }];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"法币交易与币币交易共用一个账户";
    label2.font = [UIFont systemFontOfSize:12];
    label2.textColor = RGB(30, 63, 89);
    [self.view addSubview:label2];
    label2.textAlignment = NSTextAlignmentLeft;
    
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.left.equalTo(label);
        make.width.equalTo(@(300));
        make.height.equalTo(@(20));
    }];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.text = @"手机号码";
    label3.font = [UIFont systemFontOfSize:15];
    label3.textColor = RGB(197, 206, 213);
    [self.view addSubview:label3];
    label3.textAlignment = NSTextAlignmentLeft;
    
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label2.mas_bottom).offset(30);
        make.left.equalTo(label);
        make.width.equalTo(@(70));
        make.height.equalTo(@(20));
    }];
    
    UITextField * phoneTextField=[[UITextField alloc]init];
    _phoneTextField=phoneTextField;
//    _phoneTextField.secureTextEntry = YES;
    _phoneTextField.tag=1001;
    [self.view addSubview:_phoneTextField];
    [self setTextField:_phoneTextField WithPlaceholder:@"请输入您的手机号" and:UIKeyboardTypeNumberPad];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label3.mas_centerY);
        make.left.equalTo(label3.mas_right).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(30));
    }];

    
    UIView * line1 = [[UIView alloc]init];
    [self.view addSubview:line1];
    line1.backgroundColor=RGB(230, 234, 237);
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label3.mas_bottom).offset(10);
        make.left.equalTo(label);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(0.8));
    }];
    
    UILabel *label4 = [[UILabel alloc] init];
    label4.text = @"密码";
    label4.font = [UIFont systemFontOfSize:15];
    label4.textColor = RGB(197, 206, 213);
    [self.view addSubview:label4];
    label4.textAlignment = NSTextAlignmentLeft;
    
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(20);
        make.left.equalTo(label);
        make.width.equalTo(@(70));
        make.height.equalTo(@(20));
    }];
    
    
    UITextField * passwordTextField=[[UITextField alloc]init];
    _passwordTextField=passwordTextField;
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.tag=1002;
    [self.view addSubview:_passwordTextField];
    [self setTextField:passwordTextField WithPlaceholder:@"请输入您的密码" and:UIKeyboardTypeDefault];
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label4.mas_centerY);
        make.left.equalTo(label4.mas_right).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(30));
    }];
    
    
    UIView * line2 = [[UIView alloc]init];
    [self.view addSubview:line2];
    line2.backgroundColor=RGB(230, 234, 237);
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label4.mas_bottom).offset(10);
        make.left.equalTo(label);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(0.8));
    }];
    
    
    UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"立即登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    loginBtn.backgroundColor=RGB(206, 217, 222);
    loginBtn.tag=8001;
    loginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    loginBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    _loginBtn= loginBtn;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(48);
        make.left.equalTo(label);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(50));
    }];

    UIButton * registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:RGB(99, 140, 220) forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
//    registerBtn.backgroundColor=RGB(206, 217, 222);
    registerBtn.tag=8004;
    registerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    registerBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(TopBackButtonHeight);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.equalTo(@(50));
        make.width.equalTo(@(50));
    }];
    
    
    UIButton * forgetPassWordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetPassWordBtn setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetPassWordBtn setTitleColor:RGB(117, 160, 221) forState:UIControlStateNormal];
    [forgetPassWordBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPassWordBtn];
//    forgetPassWordBtn.backgroundColor=RGB(206, 217, 222);
    forgetPassWordBtn.tag=8003;
    forgetPassWordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    forgetPassWordBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [forgetPassWordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(40);
        make.left.equalTo(self.view.mas_left).offset(20);
        
        make.height.equalTo(@(20));
        make.width.equalTo(@(80));
    }];
    
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [self.view  addGestureRecognizer:singleTapGesture];
    
}


-(void)createBackBtn{
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"" forState:UIControlStateNormal];
    [backBtn setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.imageView.frame=CGRectMake(0, 0, 18, 32);
    [backBtn setImage:[UIImage imageNamed:@"图层-6"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    backBtn.backgroundColor=MC_TEST_COLOR;
    backBtn.tag=8002;
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    backBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(TopBackButtonHeight);
        make.left.equalTo(self.view.mas_left).offset(-20);
        
        make.height.equalTo(@(32));
        make.width.equalTo(@(100));
    }];
}
#pragma mark - gesture actions
- (void)closeKeyboard:(UITapGestureRecognizer *)recognizer {
    [self.view endEditing:YES];
    //    [self.viewPop hiden];
}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    

    
        textField.placeholder=placeholder;
        textField.borderStyle = UITextBorderStyleNone;
        textField.backgroundColor=[UIColor clearColor];
        textField.font = [UIFont systemFontOfSize:15];
        textField.textColor = RGB(40, 40, 40);
        textField.textAlignment = NSTextAlignmentLeft;
        textField.returnKeyType = UIReturnKeyDone;
        textField.keyboardType = type;
        [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
        [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
        [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
   
}

-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (_phoneTextField.text.length>0&&_passwordTextField.text.length) {
        _loginBtn.backgroundColor=ThemeColor;

    }else{
        _loginBtn.backgroundColor=RGB(206, 217, 222);

    }
    if (textField.tag==1001) {

    }else if(textField.tag==1002){
        
    }
}
#pragma mark-登录
-(void)LoginAction{
    
    NSString * URL = [NSString stringWithFormat:@"%@user/login",DEFAULTSEVERBASEURL];
    NSDictionary * dic =@{@"phone":_phoneTextField.text,@"password":_passwordTextField.text,@"source":@"2"};
    __weak __typeof(self)wself = self;

    [HUD showProgress:@"" inView:self.view];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:wself.view];
        NSDictionary * dic = responseObject;
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            [HUD showSuccessMessage:dic[@"msg"]];
            NSDictionary * dicc = dic[@"data"];
            if ([dicc isKindOfClass:[NSDictionary class]]) {
                
                NSString * user_id = [NSString stringWithFormat:@"%@",dicc[@"user_id"]];
                
                NSString * username = [NSString stringWithFormat:@"%@",dicc[@"username"]];
                
                NSString * nickname = [NSString stringWithFormat:@"%@",dicc[@"nickname"]];
                
                NSString * token = [NSString stringWithFormat:@"%@",dicc[@"token"]];
                
                [LoginModel SaveKEY:DL_user_id andValue:user_id];
                [LoginModel SaveKEY:DL_username andValue:username];
                [LoginModel SaveKEY:DL_nickname andValue:nickname];
                [LoginModel SaveKEY:DL_token andValue:token];
                [LoginModel SaveKEY:DL_Phone andValue:wself.phoneTextField.text];
                
                /*
                 * 登录成功后
                 */
                [wself dismissViewControllerAnimated:YES completion:^{
                    
                }];
                
                
            }
        }else{
            [HUD showMessage:dic[@"msg"]];
        }
        
        
        
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:wself.view];

    }];

}
-(void)btnClickAction:(UIButton *)btn{
    
    
    //立即登录
    if (btn.tag==8001){
        if (_phoneTextField.text.length<1) {
            [HUD showMessage:@"请输入手机号！"];
            return;
        }
        if (_passwordTextField.text.length<1) {
            [HUD showMessage:@"请输入密码！"];
            return;
        }
        
        [self LoginAction];
        
        
        
        
    }else if(btn.tag==8002){
    //返回
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];

    }else if(btn.tag==8003){
    //忘记密码
        NSLog(@"忘记密码");
//        [HUD showMessage:@"忘记密码"];
        
    }else if (btn.tag==8004){
    //注册
//        [HUD showMessage:@"注册"];
        
        RegisterViewController * VC = [[RegisterViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
}

@end
